home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / M / MacBinary C Source.cpt / MacBinary C Source
Text File  |  1991-12-05  |  7KB  |  279 lines

  1. /*  DON'T IGNORE THESE COMMENTS!
  2. The compiled version of this file can be used to convert a
  3. MacBinary file to the forked format used by the Macintosh.
  4. This is useful if you have a modem on the PC, but no way
  5. to conveniently get MacUsable copies of UnStuffIt and MB2MAC
  6. onto the Mac.  To gain MacBinary capability, compile this and
  7. use it on both MB2MAC.SIT and UNSTUFF.SEA.  The Mac format
  8. UNSTUFF can then be used to unpack MB2MAC.SIT, which goes into
  9. the Apple File Exchange folder.  At that point, you can eliminate
  10. the extra step of using this program to convert everything.  I would
  11. recoment using MB2MAC anyway, because this program is not what
  12. I would call "Industrial Strength".
  13.  
  14. I originally wrote this program using a resource file, hence
  15. the _SOURCEONLY_ define found down below.  Transfering a resource
  16. file didn't make a whole lot of sense, so the program has been
  17. changed to allow text only uploads and downloads.
  18.  
  19. This program is hereby placed into the public domain.  If you choose
  20. to distribute it (please do!!), please distribute it unchanged.  I
  21. wrote it in Think C, but there is no compiler specific stuff except
  22. maybe the <Menus.h> include file.
  23.  
  24. This program works on my mac, therefore it should work on yours.  The
  25. only programs that I've tested it on are MB2MAC.SIT and STUFFIT.SEA.
  26. By the way, this is the first Macintosh program I've ever done.  I 
  27. haven't included any 'in progress' information, so don't panic
  28. if nothing seems to be happening if you are converting a large file.
  29. This file was originally saved using the DOS style newlines.  If you
  30. ran it through Apple File Exchange using the default translation, do
  31. it again, this time using the text translation method. 
  32.  
  33. There are two files on CIS that explain how this program does
  34. what it does.  If interested, check out MACBIN.STD and MB2STD.TXT.
  35. I've forgotten which forums they are in.
  36.  
  37. David Schooley, CIS ID: 72571,2543
  38. November 18, 1991
  39. */
  40.  
  41.  
  42. #include <Menus.h>
  43.  
  44. #define _SOURCEONLY_ 
  45.  
  46. int    MacEventCheck(void);
  47. int    DoMenu(long startPoint);
  48. void DoTranslate(void);
  49.  
  50. #define APPLEID    128
  51. #define FILEID        129
  52. #define EDITID        130
  53.         
  54. #define APPLEM    0
  55. #define FILEM    1
  56. #define EDITM    2
  57. #ifndef _SOURCEONLY_
  58. #define MENUCOUNT 3
  59. #else
  60. #define MENUCOUNT 2
  61. #endif
  62.  
  63. MenuHandle    menuList[MENUCOUNT];        
  64.  
  65. int main()
  66. {
  67.     int    fileID;
  68.     int    i;
  69.  
  70. /* Initialize the Mac */
  71.  
  72.     InitGraf(&thePort);
  73.     InitFonts();
  74.     FlushEvents(everyEvent, 0);
  75.     InitWindows();
  76.     TEInit();
  77.     InitDialogs(NULL);
  78.     InitCursor();
  79.     
  80. /* Set up the menu bar */
  81.     InitMenus();
  82.     
  83. #ifndef _SOURCEONLY_
  84. /* This section is only compiled if _SOURCEONLY_ is NOT defined    */
  85.     GetResource('MENU', fileID);
  86.     menuList[APPLEM]    = GetMenu(APPLEID);
  87.     AddResMenu(menuList[APPLEM], 'DRVR');
  88.     menuList[FILEM]        = GetMenu(FILEID);
  89.     menuList[EDITM]        = GetMenu(EDITID);
  90.     
  91. #else
  92. /* This section is compiled if _SOURCEONLY_ is defined.            */
  93.  
  94.     menuList[APPLEM]    = NewMenu(APPLEID, "\p\024");
  95.     /* I have yet to figure out why the above statement puts up    */
  96.     /* the Apple, but it works!        */
  97.     
  98.     /* Now enable the apple menu list    */
  99.     AddResMenu(menuList[APPLEM], 'DRVR');
  100.                                 
  101.     menuList[FILEM]        = NewMenu(FILEID, "\pFile");
  102.  
  103.     /* Now add the menu items */
  104.     AppendMenu(menuList[FILEM], "\pOpen/O;(-;Quit/Q");
  105.     
  106. #endif
  107.  
  108. /* Do the rest all of the time    */
  109.     for (i = 0; i < MENUCOUNT; i++)
  110.         InsertMenu(menuList[i], 0);
  111.     DrawMenuBar();
  112.     
  113.     /* Do the event loop */
  114.     while(MacEventCheck());
  115. }
  116.  
  117.  
  118.  
  119.  
  120. int MacEventCheck(void)
  121. {
  122.     EventRecord    currentEvent;
  123.     Point        mouseLoc;
  124.     WindowPtr    hotWindow;
  125.     int            result;
  126.     
  127.     
  128.     SystemTask();    /* Be nice to the rest of the system */
  129.     if(!GetNextEvent(everyEvent, ¤tEvent))
  130.         return(-1);
  131.         
  132.     /* If we are this far, that means something happened */
  133.     
  134.     switch (currentEvent.what) {
  135.     case    mouseDown:    GetMouse(&mouseLoc);
  136.                         switch(FindWindow(mouseLoc, &hotWindow)) {
  137.                         case inSysWindow:    SystemClick(¤tEvent, hotWindow);
  138.                                             break;
  139.                         
  140.                         case inMenuBar:        result = DoMenu(MenuSelect(mouseLoc));
  141.                                             break;
  142.                         
  143.                         case inDrag:    /*    DragWindow(hotWindow, mouseLoc, DragRect);*/
  144.                                             break;
  145.                         } /* End of 'FindWindow' switch */
  146.             } /* End the switch here for now */
  147.                         
  148.  
  149.     return(result);
  150. }    
  151.     
  152. int    DoMenu(long start)
  153. {
  154.     int        theItem;
  155.     int        result;
  156.     Str255    name;
  157.     
  158.     theItem = LoWord(start);
  159.     switch (HiWord(start)) {
  160.         case APPLEID:
  161.             GetItem(menuList[APPLEM], theItem, &name);
  162.             OpenDeskAcc(name);
  163.             result = -1;
  164.             break;
  165.             
  166.         case FILEID:
  167.             switch (theItem) {
  168.                 case 1:    DoTranslate();
  169.                         result = 0;
  170.                         break;
  171.                 case 3:    result = 0;
  172.                         break;
  173.             }    
  174.             break;
  175.     }
  176. }
  177.             
  178. void DoTranslate(void)
  179. {
  180.  
  181. /* Data for the input file */
  182.     SFReply    fileStruct;
  183.     Str255    inputPrompt = "Input File";
  184.     SFTypeList    typeList;
  185.     Point    diaStart = {30,30};
  186.     int        inputReferenceNum;
  187.     
  188.     char    header[128];
  189.     long    inputCount = 128;
  190.     
  191. /* Pointers to header information */
  192.     long    *fileType;
  193.     long    *fileCreator;
  194.     char    *finderFlags;
  195.     long    *dataForkLength;
  196.     long    *resForkLength;
  197.     int        *getInfoLength;
  198.  
  199. /* Other variables
  200.     long    resourceLength, dataLength;
  201.  
  202.     
  203. /* Data for the output file */
  204.     SFReply    outputFileStruct;
  205.     Str255    outputPrompt = "\0xCOutput File";
  206.     Str255    initialOut = "";
  207.     Point    diaStartOut = {30, 30};
  208.     int        outputReferenceNum;
  209.     int        outputResRefNum;
  210.     
  211.     long    dataLength, resourceLength;
  212.  
  213.     outputFileStruct.good = 0;
  214.  
  215.     SFGetFile(diaStart, inputPrompt, NULL, -1, typeList, NULL, &fileStruct);
  216.     if (fileStruct.good)  
  217.         SFPutFile(diaStartOut, outputPrompt, initialOut, NULL, &outputFileStruct);
  218.         
  219.     if (outputFileStruct.good) {
  220.         /* If we are here, then we got valid entries for both dialogs */
  221.         /* Open up the input file using the data we got from the dialog*/
  222.         if (FSOpen(fileStruct.fName, fileStruct.vRefNum, &inputReferenceNum) != noErr)
  223.             return;
  224.         /* Read in the header information */
  225.         
  226.         SetFPos(inputReferenceNum, fsFromStart, 0);
  227.         FSRead(inputReferenceNum, &inputCount, header);
  228.         fileType    = (long*) &header[65];
  229.         fileCreator    = (long*) &header[69];
  230.         finderFlags    = &header[73];
  231.         dataForkLength    = (long*) &header[83];
  232.         resForkLength    = (long*) &header[87];
  233.         getInfoLength    = (int*) &header[99];
  234.  
  235.         dataLength        = *dataForkLength;
  236.         resourceLength    = *resForkLength;
  237.         
  238.                     
  239.         /* Create the output file */
  240.         
  241.         if (Create(outputFileStruct.fName, outputFileStruct.vRefNum, *fileCreator, *fileType) != noErr)
  242.             return;
  243.         FSOpen(outputFileStruct.fName, outputFileStruct.vRefNum, &outputReferenceNum);
  244.         
  245.         /* Transfer the data fork first */        
  246.         inputCount = 128;
  247.         SetFPos(inputReferenceNum, fsFromStart, 128);
  248.         for(; dataLength > 0; dataLength -= 128) {
  249.             FSRead(inputReferenceNum, &inputCount, header);
  250.             if (dataLength >= 128)
  251.                 FSWrite(outputReferenceNum, &inputCount, header);
  252.             else
  253.                 FSWrite(outputReferenceNum, &dataLength, header);
  254.         }
  255.         
  256.         /* Now transfer the resource fork */
  257.         OpenRF(outputFileStruct.fName, outputFileStruct.vRefNum, &outputResRefNum);
  258.         for (; resourceLength > 0; resourceLength -= 128) {
  259.             FSRead(inputReferenceNum, &inputCount, header);
  260.             if (resourceLength >= 128)
  261.                 FSWrite(outputResRefNum, &inputCount, header);
  262.             else
  263.                 FSWrite(outputResRefNum, &resourceLength, header);
  264.         }
  265.         
  266.         /* All done, now close the file */
  267.         FSClose(outputReferenceNum);
  268.         FSClose(outputResRefNum);
  269.         FSClose(inputReferenceNum);
  270.         FlushVol(outputFileStruct.fName, outputReferenceNum);
  271.         FlushVol(outputFileStruct.fName, outputResRefNum);
  272.                 
  273.  
  274.     }    
  275.     
  276.     
  277. }
  278.                         
  279.